﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Ajax.Common
{
    /// <summary>
    /// 集合类
    /// </summary>
    public class MyCollections
    {

        #region 去除空项目
        /// <summary>
        /// 去除空项目
        /// </summary>
        /// <param name="InPut"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static List<string> NoEmptyItem(string InPut, char pattern)
        {
            string[] OutPut = InPut.Split(pattern);
            return NoEmptyItem(OutPut);
        }
        /// <summary>
        /// 去除空项目
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static List<string> NoEmptyItem(string[] InPut)
        {
            List<string> list = new List<string>();
            foreach (string str in InPut)
            {
                if (str.Trim().Length > 0)
                {
                    list.Add(str);
                }
            }
            return list;
        }
        /// <summary>
        /// 去除空项目
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static List<string> NoEmptyItem(List<string> InPut)
        {
            List<string> list = new List<string>();

            InPut.Remove(string.Empty);
            foreach (string str in InPut)
            {
                if (str.Trim().Length > 0)
                {
                    list.Add(str);
                }
            }
            return list;
        }
        #endregion 

        #region 去除数组的相同项,去除数组中的空项
        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="InPut">可以被转换成数组的字符串</param>
        /// <param name="pattern">分割符号</param>
        /// <returns></returns>
        public static List<string> NoSomeValue(string InPut, char pattern)
        {
            string[] arrTemp = InPut.Split(pattern);

            return NoSomeValue(arrTemp);
        }
        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static List<string> NoSomeValue(List<string> InPut)
        {
            return NoSomeValue(InPut.ToArray());
        }

        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="InPut">传入的数组</param>
        /// <returns>返回ArrayList</returns>
        public static List<string> NoSomeValue(string[] InPut)
        {
            List<string> list = new List<string>();
            if (InPut.Length > 0)
            {
                Array.Sort(InPut);

                int size = 1;
                for (int i = 1; i < InPut.Length; i++)
                    if (InPut[i] != InPut[i - 1])
                        size++;

                String[] myTempData = new String[size];

                int j = 0;

                myTempData[j++] = InPut[0];

                for (int i = 1; i < InPut.Length; i++)
                    if (InPut[i] != InPut[i - 1])
                        myTempData[j++] = InPut[i];


                list.AddRange(myTempData);
                return list;
            }

            list.AddRange(InPut);
            return list;
        }



        /// <summary>
        /// 去除数组的相同项 
        /// </summary>
        /// <param name="InPut"></param>
        /// <returns></returns>
        public static T[] NoSomeValue<T>(T[] InPut)
        {
            List<T> list = new List<T>();
            list.AddRange(InPut);
            foreach (T temp in list)
            {
                if (list.Contains(temp) == false)
                {
                    list.Add(temp);
                }
            }
            return list.ToArray();
        }
        #endregion

        #region 倒序
        /// <summary>
        /// 倒序
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string[] Reverse(string[] str)
        {
            string temp = "\0";
            int len = str.Length;
            for (int i = 0; i < len / 2; i++)
            {
                temp = str[i];
                str[i] = str[len - 1 - i];
                str[len - 1 - i] = temp;
            }

            return str;
        }
        #endregion


        /// <summary>
        /// 是否包含输入的值
        /// </summary>
        /// <param name="Orginal"></param>
        /// <param name="Matches"></param>
        /// <returns></returns>
        /// <remarks>用于取代大量的 if 代码。</remarks>
        static public bool Contain(string Orginal, params string[] Matches)
        {
            for (int Match = 0; Match < Matches.Length; ++Match)
            {
                if (Orginal.IndexOf(Matches[Match], StringComparison.CurrentCultureIgnoreCase) > -1)
                {
                    return true;
                }
            }
            return false;
        }

    }
}